﻿Imports EstoqueDAL
Public Class InventarioBLL
    Dim objDAL As InventarioDAL
#Region "Propriedades"
    Property inv_codigo As Integer
    Property inv_data As Nullable(Of DateTime)
    Property sta_codigo As Integer
    Property litens As New List(Of ItensInventarioBLL)
#End Region
    
#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, data As Nullable(Of DateTime), status As Integer)
        Me.inv_codigo = codigo
        Me.inv_data = data
        Me.sta_codigo = status
    End Sub
#End Region

#Region "Funções"
#Region "Inserir"
    Function inseriralterar(usuario As Integer) As Boolean
        Try
            objDAL = New InventarioDAL
            Me.inv_codigo = objDAL.inseriralterar(Me.inv_codigo, Me.inv_data, Me.sta_codigo, usuario)
            If (Me.inv_codigo > 0) Then
                excluiritens()
                InserirItens()
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return True
    End Function

#End Region

#Region "Excluir"
    Function excluir(usuario As Integer) As Boolean
        Try
            '4 aberto
            '6 fechado
            If (Me.sta_codigo <> 6) Then
                objDAL = New InventarioDAL
                Dim obj As New ItensInventarioBLL
                obj.inv_codigo = Me.inv_codigo
                obj.excluir()
                objDAL.excluir(Me.inv_codigo, usuario)
            Else
                Throw New Exception("Erro ao excluir inventário")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return True
    End Function

#End Region

#Region "Localizar"
    Public Function localizar(descricao As String, tipo As Integer, descricao2 As String) As DataTable
        Try
            objDAL = New InventarioDAL
            Return objDAL.buscar(descricao, tipo, descricao2)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region

#Region "Valida Campos obrigatórios"
    Public Function valida() As Boolean
        If (IsNothing(Me.inv_data)) Then Throw New Exception("Data inválida")
        If (sta_codigo <= 0) Then Throw New Exception("Preencha o status")
        Return True
    End Function
#End Region

#Region "Buscar ItensCompra"
    Public Function buscaritens() As DataTable
        Dim tab As New DataTable
        Try
            Dim objitens As New ItensInventarioBLL
            tab = objitens.localizar(Me.inv_codigo, 1)
        Catch ex As Exception
            Throw New Exception("Erro ao localizar lista de itens")
        End Try
        Return tab

    End Function
#End Region

#Region "Função atualizar restante"
    Public Sub atualiza()
        Dim tab As New DataTable
        Try
            objDAL = New InventarioDAL
            objDAL.atualizar(Me.inv_codigo)
        Catch ex As Exception
            Throw New Exception("Erro ao atualizar peças")
        End Try

    End Sub
#End Region
#End Region

#Region "Procedimentos"
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New InventarioDAL
            tab = objDAL.buscar(codigo, 1, "")
            If (tab.Rows.Count > 0) Then
                Me.inv_codigo = tab.Rows(0)("inv_codigo").ToString

                If (tab.Rows(0)("inv_data") Is DBNull.Value) Then
                    Me.inv_data = Nothing
                Else
                    Me.inv_data = tab.Rows(0)("inv_data").ToString
                End If
                Me.sta_codigo = tab.Rows(0)("sta_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Inserir ItensCompra"
    Sub InserirItens()
        Try
            For Each item As ItensInventarioBLL In litens
                item.inv_codigo = Me.inv_codigo
                item.inserir()
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

#End Region

#Region "Excluir ItensCompra"
    Sub excluiritens()
        Dim item As New ItensInventarioBLL
        item.inv_codigo = Me.inv_codigo
        item.excluir()
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.inv_codigo = Nothing
        Me.inv_data = Nothing
        Me.sta_codigo = Nothing
        Me.objDAL = Nothing
    End Sub
#End Region
#End Region
End Class
